<!-- 表单弹窗 -->
<form id="titleRule-form" lay-filter="titleRule-form" class="layui-form model-form">
    <input name="ttruid" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">规则所属称号类型</label>
        <div class="layui-input-block">
            <select name="tttype" id="tttype" lay-verify="required" lay-filter="type">
            </select>
        </div>
    </div>
    <div class="layui-form-item" id="role" hidden="hidden">
        <label class="layui-form-label">规则所属角色</label>
        <div class="layui-input-block">
            <select name="roleid" id="roleid">
                <option value="-1"></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-block">
            <input name="ttcode" placeholder="请输入规则编号" type="text" class="layui-input"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <input name="ttrule" placeholder="请输入规则描述" type="text" class="layui-input"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">条件</label>
        <div class="layui-input-block">
            <input name="points" placeholder="请输入条件值" type="text" class="layui-input"
                   lay-verify="number" required/>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="titleRule-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'config'], function () {
        let layer = layui.layer;
        let admin = layui.admin;
        let form = layui.form;
        let config = layui.config;


        // 获取所有称号类型
        layer.load(2);
        admin.req('api-user/titleType/getSelectTitleTypes', {}, function (data) {
            layer.closeAll('loading');
            if (0 == data.code) {
                $.each(data.data,function(index,item){
                    $('#tttype').append(new Option(item.tpname,item.tttpid));//往下拉菜单里添加元素
                });
                form.render();  //菜单渲染 把内容加载进去
            } else {
                layer.msg('获取称号类型失败', {icon: 2, time: 500});
            }
        }, 'post');

        // 获取所有角色
        layer.load(2);
        admin.req('api-user/roles', {}, function (data) {
            layer.closeAll('loading');
            if (0 == data.code) {
                $.each(data.data,function(index,item){
                    $('#roleid').append(new Option(item.name,item.id));//往下拉菜单里添加元素
                });
                form.render();  //菜单渲染 把内容加载进去
            } else {
                layer.msg('获取称号类型失败', {icon: 2, time: 500});
            }
        }, 'get');

        // 选择角色专属后显示角色列表
        form.on('select(type)',function () {
            var type = $("select[name=tttype]").val();
            if (type == 1){
                $("#role").show();
            } else {
                $("select[name=roleid]").val(-1);//往下拉菜单里添加元素
                form.render();  //菜单渲染 把内容加载进去
                $("#role").hide();
            }
        });





        // 填充表单数据
        let points = admin.getTempData('titleRule');
        if (points) {
            if (points.tttype === 1){
                console.log(points)
                $("#role").show();
                // $("#roleid option:selected").val(points.roleid);
                // $("select[name=roleid]").val(points.roleid);
                // form.render();
            }
            $("input[name='ttcode']").attr('disabled',true);
            form.val('titleRule-form', points);
        }
        form.render();

        // 表单提交事件
        form.on('submit(titleRule-form-submit)', function (data) {
            layer.load(2);
            if (data.field.ttruid != null && data.field.ttruid != ''){
                console.log(JSON.stringify(data.field));
                if (data.field.tttype !== '1'){
                    data.field.roleid = -1;
                }
                admin.req('api-user/titleRule/updateTitleRules', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    if (data.code === 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'POST');
                return false;
            } else {
                console.log(JSON.stringify(data.field));
                if (data.field.tttype !== '1'){
                    data.field.roleid = -1;
                }
                admin.req('api-user/titleRule/addTitleRules', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    if (data.code === 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'POST');
                return false;
            }

        });
    });
</script>
